package com.softeem.action;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;


public class BaseServlet extends HttpServlet {

    protected HttpServletRequest  req;
    protected HttpServletResponse resp;



    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //注入request、response对象 让子类使用
        this.req = req;
        this.resp = resp;

        req.setCharacterEncoding("utf-8");

        //获取访问路径userServlet/save
        String requestURI = req.getRequestURI();

        //切割save
        String[] split    = requestURI.split("/");
        String methodName = split[split.length-1];

        try {
            //根据方法名获取方法对象
            Method method = this.getClass().getMethod(methodName);
            //执行方法
            method.invoke(this);
        } catch(Exception e) {
            e.printStackTrace();
        }

    }
}
